import { CommonQueryParams, POST, Auth, QueryParams, ResponeList, PUT, GET, OptRespone, DELETE } from 'spd-request';

interface IUserDefault {
    shouldChangePasswordOnNextLogin?: boolean;
    surname?: string;
    userName?: string;
    isActive?: boolean;
    isLockoutEnabled?: boolean;
    isTwoFactorEnabled?: boolean;
}
interface IDefaultCreateOrUpdateUserParam {
    sendActivationEmail?: boolean;
    // setRandomPassword?: boolean;
    initProjectFlag?: boolean;
}
interface IUser extends IUserDefault{
    EmailAddress: string;
    name: string;
    password: string|null;
}

export interface ICreateOrUpdateUserParam extends IDefaultCreateOrUpdateUserParam{
    assignedRoleNames: string[];
    organizationUnits: number[];
    user: IUser;
}
// let dd = { 
//     "user": { 
//         "name": "韩文涛", 
//         "userName": null, 
//         "surname": null, 
//         "EmailAddress": "584@qq.com", 
//         "password": "123qwe", 
//         "isActive": true, 
//         "shouldChangePasswordOnNextLogin": true, 
//         "isTwoFactorEnabled": false, 
//         "isLockoutEnabled": false 
//     }, 
//     "assignedRoleNames": [
//         "abefbb90d23f458bbdecd1cdaefb3c99"
//     ], 
//     "sendActivationEmail": false, 
//     "setRandomPassword": false, 
//     "initProjectFlag": false, 
//     "organizationUnits": [19] 
// }

export namespace User {
    export function updateActiveStatus(userId: string | number, isActive: boolean): Promise<OptRespone<null>> {
        return PUT<any>('/User/UpdateActiveStatus?userId=' + userId, { userId: userId }).then(res => {
            if (res.success === true) {
                return { error: null, data: null }
            } else {
                return { error: '修改失败！', data: null };
            }
        }).catch(() => {
            return { error: '请求出错！', data: null }
        });
    }

    const defaultUserInfo = {
        userName: null, 
        surname: null, 
        isActive: true, 
        shouldChangePasswordOnNextLogin: true, 
        isTwoFactorEnabled: false, 
        isLockoutEnabled: false,
        password: null
    }
    const defaultCreateOrUpdateUserData = {
        sendActivationEmail: false, 
        initProjectFlag: false, 
        setRandomPassword: false
    }

    export function createOrUpdateUser(data: ICreateOrUpdateUserParam): Promise<OptRespone<null>> {
        let newData = Object.assign(defaultCreateOrUpdateUserData, data);
        newData.user = Object.assign(defaultUserInfo, newData.user);
        if(!newData.user.password) {
            newData.user.password = null;
        }
        return POST<any>('/User/CreateOrUpdateUser', newData).then(res => {
            if (res.success === true) {
                return { error: null, data: null }
            } else {
                return { error: '添加或修改人员失败！', data: null };
            }
        }).catch(() => {
            return { error: '请求出错！', data: null }
        });
    }

    export function deleteUser(param: {id: number}): Promise<OptRespone<null>> {
        return DELETE('/User/DeleteUser', {params: param}).then(res => {
            if (res.success === true) {
                return { error: null, data: null }
            } else {
                return { error: '删除失败！', data: null };
            }
        }).catch(() => {
            return { error: '请求出错！', data: null }
        });
    }

}